package model
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	public class VirtualPet extends EventDispatcher
	{	
		public static const PETSTATE_FULL:int = 0;
		public static const PETSTATE_HUNGRY:int = 1;
		public static const PETSTATE_STARVING:int = 2;
		public static const PETSTATE_DEAD:int = 3;
		public static const STATE_CHANGE:String = "STATE_CHANGE";
		public static const NAME_CHANGE:String = "NAME_CHANGE";
		
		private static var maxNameLength:int = 20;
		private static var maxCalories:int = 1500;
		private static var caloriesPerSecond:int = 100;
		
		private var name:String;
		private var calories:int;
		private var digestIntervalID:int;
		private var petState:int;
		
		public function VirtualPet(name:String)
		{
			this.name = name;
			start();
			setCalories(VirtualPet.maxCalories/2);
		}
		
		public function eat(foodItem:Food):void
		{	
			if(getPetState() == VirtualPet.PETSTATE_DEAD)
			{
				trace(name + " is dead. You can't feed him.");
				return;
			}
			
			else if((foodItem is Apple) && (foodItem as Apple).hasWorm())
			{
				trace("The " + foodItem.getName() + " has worm inside. " + getName() +" can not eat it.");
				return;
			}
			else{
				trace(getName() + " ate some "+ foodItem.getName() +".");
				setCalories(getCalories() + foodItem.getCalories());
			}
		}
		
		private function setCalories(newCurrentCalories:int):void
		{
			if(newCurrentCalories>VirtualPet.maxCalories)
			{
				calories = VirtualPet.maxCalories;
			}
			else if(newCurrentCalories<0)
			{
				calories = 0;
			}
			else
			{
				calories = newCurrentCalories;
			}
			
			var caloriesPercentage:int = Math.floor(getHunger()*100);
			
			trace(getName() + " has " + getCalories() + " calories, what is " + caloriesPercentage +"% left" );
			
			if(caloriesPercentage == 0){
				die();
			}
			else if(caloriesPercentage < 20)
			{
				setPetState(VirtualPet.PETSTATE_STARVING);
			}
			else if(caloriesPercentage < 50)
			{
				setPetState(VirtualPet.PETSTATE_HUNGRY);
			}
			else
			{
				setPetState(VirtualPet.PETSTATE_FULL);
			}
		}
		
		private function die():void
		{
			setPetState(VirtualPet.PETSTATE_DEAD);
			stop();
			trace(name + " has died.");
		}
		
		public function stop():void
		{
			clearInterval(digestIntervalID);
		}
		
		public function start():void
		{
			stop();
			digestIntervalID = setInterval(digest, 1000);
		}
		
		private function digest():void
		{
			trace(getName() + " digest some food");
			setCalories(getCalories() - caloriesPerSecond);
		}
		
		public function getCalories():int
		{
			return calories;
		}
		
		public function getHunger():Number
		{
			return getCalories()/VirtualPet.maxCalories;
		}
		
		public function setName(newName:String):void
		{
			if(newName.length>VirtualPet.maxNameLength)
				newName = newName.substring(0, VirtualPet.maxNameLength);
			else if(newName == "")
				return;
			name = newName;
			dispatchEvent(new Event(VirtualPet.NAME_CHANGE));
		}
		
		public function getName():String
		{
			return name;
		}
		
		private function setPetState(newPetState:int):void{
			if(newPetState == petState)
				return;
			petState = newPetState;
			dispatchEvent(new Event(VirtualPet.STATE_CHANGE));
		}
		
		public function getPetState():int
		{
			return petState;
		}
	}
}